import re
from disn.packer import unpack
from urllib.parse import unquote, quote
from jd.client import JDClient
# LINKSTARS_URL = 'https://www.linkstars.com/click.php?feedback=%s_0_3117__ztm&to=%s'
LINKSTARS_URL = 'http://union.linkstars.com/click.php?feedback=%s_0_3117__ztm&to=%s'

def get_js_content(smzdm):
    m = re.search(r'(eval.+?\{\}\)\))', smzdm.remark)
    if m:
        smzdm.remark = m.group(1)

def unpacker_js_content(smzdm):
    smzdm.remark = unpack(smzdm.remark)

def update_sku_and_url(smzdm):
    mall = smzdm.article_mall
    _url = ''
    if mall.name == '京东' or mall.name == '京东全球购':
        jd = JDClient()
        m = re.search(r'smzdmhref1=.*?(https?.*?to=(https?.+?(2F)?(\d+)+\.html?))', smzdm.remark)
        if not smzdm.active and m:
            if m.group(2) and m.group(4):
                smzdm.commodity_sku = m.group(4)
                _url = m.group(2)
                smzdm.commodity_url = unquote(_url)
                commodity_url_mb = 'https://wq.jd.com/item/view?sku=%s' %smzdm.commodity_sku
                try:
                    pc_ret_dict = jd.get_promotion_code(smzdm.commodity_url, 'PC') # pc推广
                    if 'code' in pc_ret_dict and pc_ret_dict['code'] == '0': # JD offical promotion url
                        smzdm.commodity_url_refer = pc_ret_dict['url']
                    else: # Linkstars promotion urls
                        print('Get JD offical CPS link failure,Code=%s,\n%s' %(pc_ret_dict['code'], pc_ret_dict['msg']))
                        smzdm.commodity_url_refer = LINKSTARS_URL %(mall.plan_code, quote(smzdm.commodity_url,'')) # quote(smzdm.commodity_url,'')
                    mb_ret_dict = jd.get_promotion_code(commodity_url_mb, 'WL') # 无线推广
                    if 'code' in mb_ret_dict and mb_ret_dict['code'] == '0': # JD offical promotion url
                        smzdm.commodity_url_refer_mb = mb_ret_dict['url']
                    else:
                        print('Get JD offical CPS link failure,Code=%s,\n%s' %(mb_ret_dict['code'], mb_ret_dict['msg']))
                        smzdm.commodity_url_refer_mb = LINKSTARS_URL %(mall.plan_code_wx, quote(commodity_url_mb,'')) #
                except Exception as e:
                    print('Get JD offical CPS link failure,', e)
                    smzdm.commodity_url_refer = LINKSTARS_URL %(mall.plan_code, quote(smzdm.commodity_url,'')) # quote(smzdm.commodity_url,'')
                    smzdm.commodity_url_refer_mb = LINKSTARS_URL %(mall.plan_code_wx, quote(commodity_url_mb,'')) #
            if smzdm.commodity_sku: smzdm.active = True

        # https://sale.jd.com/act/q1dJbiNofewO.html
        m = re.search(r'smzdmhref1=.*?(https?.*?feedback=(.*?)&to=(.*?html?))', smzdm.remark)
        if not smzdm.active and m:
            if m.group(3):
                _url = m.group(3)
                smzdm.commodity_url = unquote(_url)
                try:
                    pc_ret_dict = jd.get_promotion_code(smzdm.commodity_url, 'PC') # pc推广
                    if 'code' in pc_ret_dict and pc_ret_dict['code'] == '0': # JD offical promotion url
                        smzdm.commodity_url_refer = pc_ret_dict['url']
                    else: # Linkstars promotion urls
                        print('Get JD offical CPS link failure,Code=%s,\n%s' %(pc_ret_dict['code'], pc_ret_dict['msg']))
                        smzdm.commodity_url_refer = LINKSTARS_URL %(mall.plan_code, quote(smzdm.commodity_url,'')) # quote(smzdm.commodity_url,'')
                    mb_ret_dict = jd.get_promotion_code(smzdm.commodity_url, 'WL') # 无线推广
                    if 'code' in mb_ret_dict and mb_ret_dict['code'] == '0': # JD offical promotion url
                        smzdm.commodity_url_refer_mb = mb_ret_dict['url']
                    else:
                        print('Get JD offical CPS link failure,Code=%s,\n%s' %(mb_ret_dict['code'], mb_ret_dict['msg']))
                        smzdm.commodity_url_refer_mb = LINKSTARS_URL %(mall.plan_code_wx, quote(smzdm.commodity_url,''))
                except Exception as e:
                    print('Get JD offical CPS link failure,', e)
                    smzdm.commodity_url_refer = LINKSTARS_URL %(mall.plan_code, quote(smzdm.commodity_url,'')) # quote(smzdm.commodity_url,'')
                    smzdm.commodity_url_refer_mb = LINKSTARS_URL %(mall.plan_code_wx, quote(smzdm.commodity_url,''))
            if smzdm.commodity_url:
                smzdm.active = True
                smzdm.special = True

        # https://wqitem.jd.com/item/view?send_info=694cee3690de18ba85cfa7ee5c0862c07e33e9a5152a6b0d&_wv=1025&ptag=17006.10.23&sku=1221439
        m = re.search(r'smzdmhref1=.*?https?.+?to=(https?.*?)send_info.*?(sku.*?(\d{5,12})+)+', smzdm.remark)
        if not smzdm.active and m:
            if m.group(3):
                smzdm.commodity_sku = m.group(3)
                _url = '%s%s' %(m.group(1), m.group(2))
                # smzdm.commodity_url_mb = unquote(_url)
                smzdm.commodity_url = 'https://item.jd.com/%s.html' % smzdm.commodity_sku
                commodity_url_mb = 'https://wq.jd.com/item/view?sku=%s' %smzdm.commodity_sku
                try:
                    pc_ret_dict = jd.get_promotion_code(smzdm.commodity_url, 'PC') # pc推广
                    if 'code' in pc_ret_dict and pc_ret_dict['code'] == '0': # JD offical promotion url
                        smzdm.commodity_url_refer = pc_ret_dict['url']
                    else: # Linkstars promotion urls
                        print('Get JD offical CPS link failure,Code=%s,\n%s' %(pc_ret_dict['code'], pc_ret_dict['msg']))
                        smzdm.commodity_url_refer = LINKSTARS_URL %(mall.plan_code, quote(smzdm.commodity_url,'')) # quote(smzdm.commodity_url,'')
                    mb_ret_dict = jd.get_promotion_code(commodity_url_mb, 'WL') # 无线推广
                    if 'code' in mb_ret_dict and mb_ret_dict['code'] == '0': # JD offical promotion url
                        smzdm.commodity_url_refer_mb = mb_ret_dict['url']
                    else:
                        print('Get JD offical CPS link failure,Code=%s,\n%s' %(mb_ret_dict['code'], mb_ret_dict['msg']))
                        smzdm.commodity_url_refer_mb = LINKSTARS_URL %(mall.plan_code_wx, quote(commodity_url_mb,'')) #
                except Exception as e:
                    print('Get JD offical CPS link failure,', e)
                    smzdm.commodity_url_refer = LINKSTARS_URL %(mall.plan_code, quote(smzdm.commodity_url,'')) # quote(smzdm.commodity_url,'')
                    smzdm.commodity_url_refer_mb = LINKSTARS_URL %(mall.plan_code_wx, quote(smzdm.commodity_url,''))
                # smzdm.recommend_url_mb = LINKSTARS_URL %(mall.plan_code, _url)
            if smzdm.commodity_sku: smzdm.active = True

    elif mall.name == '苏宁易购' or mall.name == '苏宁易购海外购':
        m = re.search(r'smzdmhref1=.*?(https?.*?to=(https?.+?(2F)?(\d+)+\.html?))', smzdm.remark)
        if not smzdm.active and m:
            if m.group(2) and m.group(4):
                smzdm.commodity_sku = m.group(4)
                _url = m.group(2)
                smzdm.commodity_url = unquote(_url)
                if smzdm.commodity_url.find('product.suning') >= 0:
                    # smzdm.commodity_url_mb = smzdm.commodity_url.replace('product.suning.com','m.suning.com/product')
                    smzdm.commodity_url_refer = LINKSTARS_URL %(mall.plan_code, quote(smzdm.commodity_url,''))
                    # smzdm.recommend_url_mb = LINKSTARS_URL %(mall.plan_code, quote(smzdm.commodity_url_mb,''))
            if smzdm.commodity_sku: smzdm.active = True
        m = re.search(r'smzdmhref1=.*?(https?.+?to=(https?.+?html?))', smzdm.remark)
        # http://yzdh.suning.com/yzdh-web/m/exchange/voucher821.htm
        if not smzdm.active and m:
            _url = m.group(2)
            smzdm.commodity_url = unquote(_url)
            smzdm.commodity_url_refer = LINKSTARS_URL %(mall.plan_code, quote(smzdm.commodity_url,''))
            # smzdm.recommend_url_mb = LINKSTARS_URL %(mall.plan_code, quote(smzdm.commodity_url,''))
            if smzdm.commodity_url:
                smzdm.active = True
                smzdm.special = True
        m = re.search(r"smzdmhref1=.+?to=(https?.+?quan.+?)\\';ga", smzdm.remark)
        # http://quan.suning.com/lqzx_recommend.do?activityId=201707110001499591&activitySecretKey=vjBBGdgVPeweYfP8YM3XLgqr
        if not smzdm.active and m:
            _url = m.group(1)
            smzdm.commodity_url = unquote(_url)
            # smzdm.commodity_url_mb = unquote(_url)
            smzdm.commodity_url_refer = LINKSTARS_URL %(mall.plan_code, quote(smzdm.commodity_url,''))
            # smzdm.recommend_url_mb = LINKSTARS_URL %(mall.plan_code, quote(smzdm.commodity_url,''))
            if smzdm.commodity_url:
                smzdm.active = True
                smzdm.special = True

    elif mall.name == '亚马逊中国' or mall.name == '亚马逊海外购' or mall.name == '美国亚马逊' or mall.name == '日本亚马逊' or mall.name == '英国亚马逊':
        m = re.search(r'smzdmhref1=.*?(http.*?amazon.*?[dp|gp]+\/(\w+\/)?(\w+)+)', smzdm.remark)
        if not smzdm.active and m:
            smzdm.commodity_sku = m.group(3)
            smzdm.commodity_url = m.group(1)
            if smzdm.commodity_sku: smzdm.active = True
        m = re.search(r"smzdmhref1=.*?(https?.*?amazon.*?)\\';", smzdm.remark) # 促销专场页面
        if not smzdm.active and m:
            smzdm.commodity_url = m.group(1)
            smzdm.active = True
            smzdm.special = True
    elif mall.name == '1号店':
        m = re.search(r'smzdmhref1=.*?(https?.*?to=(https?.+?yhd.+?2F(\d+)+))', smzdm.remark)
        if not smzdm.active and m:
            smzdm.commodity_sku = m.group(3)
            _url = m.group(2)
            smzdm.commodity_url = unquote(_url)
            smzdm.commodity_url_refer = LINKSTARS_URL %(mall.plan_code, quote(smzdm.commodity_url,''))
            # if smzdm.commodity_url.find('www') >= 0:
                # smzdm.commodity_url_mb = smzdm.commodity_url.replace('www','m')
                # smzdm.recommend_url_mb = LINKSTARS_URL %(mall.plan_code, quote(smzdm.commodity_url_mb,''))
            if smzdm.commodity_sku: smzdm.active = True
    elif mall.name == '网易考拉海购' or mall.name == '网易考拉':
        m = re.search(r'smzdmhref1=.*?(https?.*?to=(https?.+?kaola.+?2F(\d+)+\.html?)+)', smzdm.remark)
        if not smzdm.active and m:
            smzdm.commodity_sku = m.group(3)
            _url = m.group(2)
            smzdm.commodity_url = unquote(_url)
            smzdm.commodity_url_refer = LINKSTARS_URL %(mall.plan_code, quote(smzdm.commodity_url,''))
            # if smzdm.commodity_url.find('www') >= 0:
                # smzdm.commodity_url_mb = smzdm.commodity_url.replace('www','m')
                # smzdm.recommend_url_mb = LINKSTARS_URL %(mall.plan_code, quote(smzdm.commodity_url_mb,''))
            if smzdm.commodity_sku: smzdm.active = True
    elif mall.name == '网易严选':
        m = re.search(r'smzdmhref1=.*?(https?.*?to=(https?.+?you.163.com.+?detail.*?id%3D(\d+)+))', smzdm.remark)
        if not smzdm.active and m:
            smzdm.commodity_sku = m.group(3)
            _url = m.group(2)
            smzdm.commodity_url = unquote(_url)
            smzdm.commodity_url_refer = LINKSTARS_URL %(mall.plan_code, quote(smzdm.commodity_url,''))
            if smzdm.commodity_sku: smzdm.active = True
    elif mall.name == '国美在线' or mall.name == '国美':
        m = re.search(r'smzdmhref1=.*?(https?.*?to=(https?.+?gome.+?2F([a-zA-Z0-9-]+)+\.html?))', smzdm.remark)
        if not smzdm.active and m:
            smzdm.commodity_sku = m.group(3)
            _url = m.group(2)
            smzdm.commodity_url = unquote(_url)
            smzdm.commodity_url_refer = LINKSTARS_URL %(mall.plan_code, quote(smzdm.commodity_url,''))
            if smzdm.commodity_sku: smzdm.active = True
        m = re.search(r'smzdmhref1=.*?(https?.*?gome.com.cn/([a-zA-Z0-9-]+)+\.html?)', smzdm.remark)
        if not smzdm.active and m:
            smzdm.commodity_sku = m.group(2)
            _url = m.group(1)
            smzdm.commodity_url = unquote(_url)
            smzdm.commodity_url_refer = LINKSTARS_URL %(mall.plan_code, quote(smzdm.commodity_url,''))
            if smzdm.commodity_sku: smzdm.active = True
    elif mall.name == '中粮我买网':
        m = re.search(r'smzdmhref1=.*?(https?.*?to=(https?.+?womai.+?2FProduct\-([a-zA-Z0-9-]+)+\.html?))', smzdm.remark)
        if not smzdm.active and m:
            smzdm.commodity_sku = m.group(3)
            _url = m.group(2)
            smzdm.commodity_url = unquote(_url)
            smzdm.commodity_url_refer = LINKSTARS_URL %(mall.plan_code, quote(smzdm.commodity_url,''))
            if smzdm.commodity_sku: smzdm.active = True
    elif mall.name == '当当' or mall.name == '当当海外购':
        m = re.search(r'smzdmhref1=.*?(https?.*?to=(https?.+?dangdang.+?2F([a-zA-Z0-9-]+)+\.html?))', smzdm.remark)
        if not smzdm.active and m:
            smzdm.commodity_sku = m.group(3)
            _url = m.group(2)
            smzdm.commodity_url = unquote(_url)
            smzdm.commodity_url_refer = LINKSTARS_URL %(mall.plan_code, quote(smzdm.commodity_url,''))
            if smzdm.commodity_sku: smzdm.active = True
        m = re.search(r"smzdmhref1=.+?backurl=(.+?2F(\d+)\.html?)", smzdm.remark)
        if not smzdm.active and m:
            smzdm.commodity_sku = m.group(2)
            _url = m.group(1)
            smzdm.commodity_url = unquote(_url)
            smzdm.commodity_url_refer = LINKSTARS_URL %(mall.plan_code, quote(smzdm.commodity_url,''))
            if smzdm.commodity_sku: smzdm.active = True
    elif mall.name == 'bodyguard apotheke中文官网':
        m = re.search(r'smzdmhref1=.*?(https?.*?to=(https?.+?ba.+?2F([a-zA-Z0-9-]+)+\.html?))', smzdm.remark)
        if not smzdm.active and m:
            smzdm.commodity_sku = m.group(3)
            _url = m.group(2)
            smzdm.commodity_url = unquote(_url)
            smzdm.commodity_url_refer = LINKSTARS_URL %(mall.plan_code, quote(smzdm.commodity_url,''))
            if smzdm.commodity_sku: smzdm.active = True
    elif mall.name == '德国药房apo.com':
        m = re.search(r'smzdmhref1=.*?(https?.*?to=(https?.+?apo.+?2F([a-zA-Z0-9-]+)+\.html?))', smzdm.remark)
        if not smzdm.active and m:
            smzdm.commodity_sku = m.group(3)
            _url = m.group(2)
            smzdm.commodity_url = unquote(_url)
            smzdm.commodity_url_refer = LINKSTARS_URL %(mall.plan_code, quote(smzdm.commodity_url,''))
            if smzdm.commodity_sku: smzdm.active = True
